package com.xdja;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.*;

/**
 * description
 *
 * @author zk
 * @since 2018/9/25
 */
public class MockClientStart {
    public static void main(String[] args) throws InterruptedException {
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(new NioEventLoopGroup())
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<Channel>() {
                    @Override
                    protected void initChannel(Channel ch) throws Exception {
                        ch.pipeline().addLast(new HttpClientCodec()).addLast(new SimpleChannelInboundHandler<HttpResponse>() {
                            @Override
                            protected void channelRead0(ChannelHandlerContext ctx, HttpResponse msg) throws Exception {
                                System.out.println("debug");
                            }
                        });
                    }
                });
        ChannelFuture channelFuture = bootstrap.connect("127.0.0.1",8989).addListener((ChannelFutureListener) future -> {
            if(future.isSuccess()){
                HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,HttpMethod.GET,"http://www.baidu.com");
                future.channel().writeAndFlush(httpRequest);
            }
        });
        channelFuture.channel().closeFuture().sync();
    }
}
